/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entitas;

import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityNotFoundException;
import javax.persistence.Persistence;
import javax.persistence.Query;
import jpa.exceptions.NonexistentEntityException;

/**
 *
 * @author Tommy
 */
public class DaftarBookingBuku {
    /* menghubungkan dengan database sesuai dengan
     * konfigurasi pada persistence unit
     */
    public DaftarBookingBuku() {
        emf = Persistence.createEntityManagerFactory("persistence");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    /* method yang digunakan untuk mengecek keberadaan satu baris
     * pada tabel buku berdasarkan parameter namaBuku
     */
    public boolean checkBooking(String namaBuku) {
        boolean result = false;
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("SELECT count(o) FROM BookingBuku AS o WHERE o.namaBuku=:bb");
            q.setParameter("bb", namaBuku);
            int jumlahUser = ((Long) q.getSingleResult()).intValue();
            if (jumlahUser > 0) {
                result = true;
            }
        } finally {
            if (em != null) {
                em.close();
            }
        }
        return result;
    }

    /* method untuk mengambil satu baris buku
     * berdasarkan parameter namaBuku
     */
    public BookingBuku getBookingBuku(String namaBuku) {
        BookingBuku booking = null;
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("SELECT object(o) FROM BookingBuku AS o WHERE o.namaBuku=:bb");
            q.setParameter("bb", namaBuku);
            booking = (BookingBuku) q.getSingleResult();
        } finally {
            if (em != null) {
                em.close();
            }
        }
        return booking;
    }

    /* method untuk mengambil semua baris dari tabel buku
     */
    public List<BookingBuku> getBookingBooks() {
        List<BookingBuku> bookingbooks = new ArrayList<BookingBuku>();

        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("SELECT object(o) FROM BookingBuku AS o");
            bookingbooks = q.getResultList();

        } finally {
            if (em != null) {
                em.close();
            }
        }
        return bookingbooks;
    }

    /* method untuk mengganti nilai atribut pada baris yang sudah ada pada tabel buku
     */
    public void editBookingBuku(BookingBuku booking) {
        EntityManager em = getEntityManager();
        em.getTransaction().begin();
        try {
            em.merge(booking);
            em.getTransaction().commit();
        } catch (Exception e) {
            em.getTransaction().rollback();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    /* method untuk memasukkan baris baru
     ke dalam tabel buku
     */
    public void createBookingBuku(BookingBuku bookingBuku) {
        EntityManager em = getEntityManager();
        em.getTransaction().begin();
        try {
            em.persist(bookingBuku);
            em.getTransaction().commit();
        } catch (Exception e) {
            em.getTransaction().rollback();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    /* method untuk menghapus satu baris pada tabel buku
     */
    public void deleteBookingBuku(int id) throws NonexistentEntityException {
        EntityManager em = getEntityManager();
        em.getTransaction().begin();
        try {
            BookingBuku bookingBuku;
            try {
                bookingBuku = em.getReference(BookingBuku.class, id);
                bookingBuku.getIdBooking();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The user with id " + id +
                        " no longer exists.", enfe);
            }
            em.remove(bookingBuku);
            em.getTransaction().commit();
        } catch (Exception e) {
            em.getTransaction().rollback();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

}
